package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentServicr {

    private  StudentDao dao = new StudentDao();
    public boolean addStudent(Student stu) {


        return dao.addStudent(stu);
    }

    public boolean isExists(String id) {

        Student [] students = dao.findAllStudent();

        boolean exists = false;
        //遍历数组

        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && id.equals(student.getId())){
                exists = true;
            }
        }
        return  exists;
    }

    public Student[] findAllStudent() {
        Student [] stu =dao.findAllStudent();
        boolean choice = false;
        //判断数组中有无对象
        for (int i = 0; i < stu.length; i++) {
            Student stus = stu[i];

            //判断有无
            if (stus != null){
                choice = true;
            }
        }
        //根据有无对象 返回相应的值
        if (choice){ //有,返回数组地址值
            return stu;
        }else {  // 无 返回null
            return null;
        }

    }

    public void deleteStudent(String delId) {
        dao.deleteStudent(delId);
    }

    public void updateStudent(Student newStu, String updateId) {
        dao.updateStudent( newStu, updateId);
    }
}
